package com.huatai.auth.controller;


import com.huatai.common.api.R;
import com.huatai.common.error.exception.ServiceException;
import com.huatai.user.dubbo.UserDubboApi;
import com.huatai.user.feign.UserClient;
import com.huatai.user.vo.UserVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;


/**
 * @program: ht-system
 * @description: ces
 * @author: songxiude
 * @create: 2023-01-31 09:47
 **/
@RestController
@RequestMapping("/test")
@Api(value = "测试", tags = "测试")
@Slf4j
public class TestController {
	@Autowired
	private UserClient userClient;

	@Autowired
	private StringRedisTemplate redisTemplate;

	@GetMapping("/test1")
	public String test1(@RequestParam("name") String name){
		log.info(name);
		log.warn(name);
		log.error(name);
		return "服务测试："+name;
	}

//	@DubboReference
//	private UserDubboApi userDubboApi;
    @GetMapping("/test")
    public R test(){
        int i=1/0;

        return R.data("ssss");
    }
	@GetMapping("/user")
	public R getUser(@RequestParam("account") String account){
		R<UserVo> user = userClient.getUserPermissions(account);
		return user;
	}
	@GetMapping("/dubbouser")
	public R getDubboUser(@RequestParam("account") String account){
		return R.status(true);
//		return R.data(userDubboApi.findUserInfo(account));
	}

	//获取uuid
	@GetMapping("getUUID")
	public R getUUID(){
		String replace = UUID.randomUUID().toString().replace("-", "");
		redisTemplate.opsForValue().set("uuidtest",replace,20, TimeUnit.MINUTES);
		return R.data(replace);
	}

	//幂等测试
	@GetMapping("/redisMd")
	public R redisMd(@RequestParam("key") String key){
		//原子性操作
		String sc="if redis.call('get',KEYS[1])==ARGV[1] then return redis.call('del',KEYS[1]) else return 0 end";

		Long aLong = redisTemplate.execute(new DefaultRedisScript<Long>(sc, Long.class), Arrays.asList("uuidtest"), key);
		if(aLong.longValue()==1L){
			System.out.println("原子性成功："+aLong);
		}else {
			System.out.println("原子性失败："+aLong);
		}
		return R.data(aLong);
	}
}
